using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Framework
{
    public static class ReflectExtension
    {
        public static bool IsStatic(this PropertyInfo propertyInfo)
        {
            var method = propertyInfo.GetGetMethod();

            return method.IsStatic;
        }

        public static bool HasGenericIEnumerable(this Type type)
        {
            return type.GetInterfaces()
                .Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof (IEnumerable<>));
        }
        public static Type GetGenericIEnumerableType(this Type type)
        {
            if (type.HasGenericIEnumerable())
            {
                var itemType = type.GetInterfaces()
                    .FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof (IEnumerable<>));
                
                return itemType == null ? null : itemType.GetGenericArguments()[0];
            }
            return null;
        }
    }
}